<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 连续字母长度（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个字符串&#xff0c;只包含大写字母&#xff0c;求在包含同一字母的子串中&#xff0c;长度第 k 长的子串的长度&#xff0c;相同字母只取最长的那个子串。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行有一个子串(1&lt;长度&lt;&#61;100)&#xff0c;只包含大写字母。</p> 
<p>第二行为 k的值</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出连续出现次数<strong>第k多</strong>的字母的次数。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:67px;">输入</td><td style="width:431px;"> <p>AAAAHHHBBCDHHHH<br /> 3</p> </td></tr><tr><td style="width:67px;">输出</td><td style="width:431px;">2</td></tr><tr><td style="width:67px;">说明</td><td style="width:431px;"> <p>同一字母连续出现的最多的是A和H&#xff0c;四次&#xff1b;</p> <p>第二多的是H&#xff0c;3次&#xff0c;但是H已经存在4个连续的&#xff0c;故不考虑&#xff1b;</p> <p>下个最长子串是BB&#xff0c;所以最终答案应该输出2。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:68px;">输入</td><td style="width:430px;"> <p>AABAAA<br /> 2</p> </td></tr><tr><td style="width:68px;">输出</td><td style="width:430px;">1</td></tr><tr><td style="width:68px;">说明</td><td style="width:430px;"> <p>同一字母连续出现的最多的是A&#xff0c;三次&#xff1b;</p> <p>第二多的还是A&#xff0c;两次&#xff0c;但A已经存在最大连续次数三次&#xff0c;故不考虑&#xff1b;</p> <p>下个最长子串是B&#xff0c;所以输出1。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:67px;">输入</td><td style="width:431px;">ABC<br /> 4</td></tr><tr><td style="width:67px;">输出</td><td style="width:431px;">-1</td></tr><tr><td style="width:67px;">说明</td><td style="width:431px;">只含有3个包含同一字母的子串&#xff0c;小于k&#xff0c;输出-1</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:68px;">输入</td><td style="width:430px;">ABC<br /> 2</td></tr><tr><td style="width:68px;">输出</td><td style="width:430px;">1</td></tr><tr><td style="width:68px;">说明</td><td style="width:430px;">三个子串长度均为1&#xff0c;所以此时k &#61; 1&#xff0c;k&#61;2&#xff0c;k&#61;3这三种情况均输出1。特此说明&#xff0c;避免歧义。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题第一个用例感觉也有歧义。</p> 
<p>用例1&#xff0c;要求“AAAAHHHBBCDHHHH”中  重复度第3大的子串。其中&#xff1a;</p> 
<ul><li>重复度第1大子串是&#xff1a;AAAA和HHHH</li><li><s>重复度第2大子串是&#xff1a;HHH&#xff0c;由于字母H已经有了更大的重复度&#xff0c;因此HHH子串不计入比较</s></li></ul> 
<p>接下来是&#xff0c;实际上的第3大&#xff0c;名义上的第2大子串&#xff1a;BB</p> 
<p>而用例1输出的第k&#61;3大重复度的子串的长度是2&#xff0c;那就肯定是指BB子串。即要求“实际”第k大&#xff1f;而不是“名义”第k大&#xff1f;</p> 
<p></p> 
<p>但是用例2&#xff0c;要求“AABAAA”中&#xff0c;重复度第2大的子串。其中&#xff1a;</p> 
<ul><li>重复度第1大子串是&#xff1a;AAA</li><li><s>重复度第2大子串是&#xff1a;AA&#xff0c;由于字母A已经有了更大的重复度&#xff0c;因此HHH子串不计入比较</s></li></ul> 
<p>接下来是&#xff0c;实际上的第3大&#xff0c;名义上的第2大子串&#xff1a;B</p> 
<p>而用例1输出的第k&#61;2大重复度的子串的长度是1&#xff0c;这里又是取名义上第k大了&#xff1f;</p> 
<p></p> 
<p>或者本题还有另一种思路&#xff0c;那就是第k大&#xff0c;就是单纯按重复度降序后的第k个。</p> 
<p>比如用例1&#xff1a; A:4&#xff0c;H:4&#xff0c;B:2&#xff0c;C:1&#xff0c;D:1</p> 
<p>这里第k&#61;3大&#xff0c;那就是B&#xff0c;子串长度为2</p> 
<p>比如用例2&#xff1a;A:3&#xff0c;B:1</p> 
<p>这里第k&#61;2大&#xff0c;那就是B&#xff0c;子串长度为1</p> 
<p>下面代码就是按照这个思路实现的。</p> 
<hr /> 
<p>2023.06.19</p> 
<p>增加处理下 k &lt;&#61; 0的情况&#xff0c;此时直接返回-1</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String s &#61; sc.next();
    int k &#61; sc.nextInt();
    System.out.println(getResult(s, k));
  }

  public static int getResult(String s, int k) {
    if (k &lt;&#61; 0) return -1;

    s &#43;&#61; &#34;0&#34;;

    HashMap&lt;Character, Integer&gt; count &#61; new HashMap&lt;&gt;();

    char b &#61; s.charAt(0);
    int len &#61; 1;

    for (int i &#61; 1; i &lt; s.length(); i&#43;&#43;) {
      char c &#61; s.charAt(i);

      if (b &#61;&#61; c) {
        len&#43;&#43;;
      } else {
        if (!count.containsKey(b) || count.get(b) &lt; len) {
          count.put(b, len);
        }
        len &#61; 1;
        b &#61; c;
      }
    }

    Integer[] arr &#61; count.values().toArray(new Integer[0]);

    if (k &gt; arr.length) return -1;
    else {
      Arrays.sort(arr, (x, y) -&gt; y - x);
      return arr[k - 1];
    }
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const { count } &#61; require(&#34;console&#34;);
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    console.log(getResult(lines[0], lines[1]));
    lines.length &#61; 0;
  }
});

/* 算法逻辑 */
function getResult(s, k) {
  if (k &lt;&#61; 0) return -1;

  s &#43;&#61; &#34;0&#34;;

  let count &#61; {};

  let b &#61; s[0];
  let len &#61; 1;

  for (let i &#61; 1; i &lt; s.length; i&#43;&#43;) {
    const c &#61; s[i];

    if (b &#61;&#61; c) {
      len&#43;&#43;;
    } else {
      if (count[b] &#61;&#61; undefined || count[b] &lt; len) {
        count[b] &#61; len;
      }
      len &#61; 1;
      b &#61; c;
    }
  }

  const arr &#61; Object.values(count);

  if (k &gt; arr.length) {
    return -1;
  } else {
    arr.sort((a, b) &#61;&gt; b - a);
    return arr[k - 1];
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()
k &#61; int(input())


# 算法入口
def getResult():
    global s
    global k

    if k &lt;&#61; 0:
        return -1

    s &#43;&#61; &#34;0&#34;

    count &#61; {}

    b &#61; s[0]
    long &#61; 1

    for i in range(1, len(s)):
        c &#61; s[i]

        if b &#61;&#61; c:
            long &#43;&#61; 1
        else:
            if count.get(b) is None or count[b] &lt; long:
                count[b] &#61; long
            long &#61; 1
            b &#61; c

    arr &#61; list(count.values())

    if k &gt; len(arr):
        return -1
    else:
        arr.sort(reverse&#61;True)
        return arr[k - 1]


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>